﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Ball : MonoBehaviour
{

    public GameObject PlatfromPre;//生成的平台预制体
    private GameObject LastPlatfrom;//前一个平台
    private Rigidbody _rigidbody;//小球的刚体组件
    private BALL_STATE _ballState = BALL_STATE.Right;//小球的运动方向
    private bool isStart = false;//游戏是否开始
    public float _speed = 0.5f;//小球的速度
    enum BALL_STATE
    {
        Forward,
        Right
    }
	void Start () {
        _rigidbody = GetComponent<Rigidbody>();//得到刚体组件                                        
        for (int i = 0; i < 40; i++)//生成的个数必须小于对象池里个数
        {
            if (i == 0)
            {
                LastPlatfrom = PoolManager._Instance.GetObject("Platfrom");
            }
            else
            {                                                                                         
                PlatformSpwn();                                         
            } 
        }
    }                
	
	
	void Update () {
        if(!EventSystem.current.IsPointerOverGameObject())
            OnTap();
	}
    /// <summary>
    ///根据鼠标的点击来改变游戏的开始和小球的运动的方向
    /// </summary>
    void OnTap()
    {
        //检测玩家是否点击
        if (Input.GetMouseButtonDown(0))
        {
            if (!isStart)
            {
                isStart = true;//当开始了过后点击鼠标就不会进入该判断
                _rigidbody.velocity = new Vector3(_speed, 0, 0);//给小球一个初始速度
                return;
            }
            switch (_ballState)//判断小球现在的状态
            {
                case BALL_STATE.Forward://小球前一个运动状态为前
                    _ballState = BALL_STATE.Right;//点击过后变成右
                    _rigidbody.velocity = new Vector3(_speed, 0, 0);//给一个向右的方向
                    break;
                case BALL_STATE.Right://小球前一个运动状态为右
                    _ballState = BALL_STATE.Forward;//点击过后变成右
                    _rigidbody.velocity = new Vector3(0, 0, _speed);//给一个向前的方向
                    break;
                default:
                    break;
            }
        }

    }
    /// <summary>
    /// 平台的生成
    /// </summary>
    void PlatformSpwn()
    {
        if (Random.Range(0, 2) == 0)//产生随机数 设置生成的位置
        {
            //实例化一个平台 并且该平台设置为上一个平台
            //LastPlatfrom = Instantiate(PlatfromPre, LastPlatfrom.transform.position + new Vector3(1, 0, 0), Quaternion.identity);
            GameObject temp = PoolManager._Instance.GetObject("Platfrom");
            temp.transform.position = LastPlatfrom.transform.position + new Vector3(1, 0, 0);
            LastPlatfrom = temp;
        }
        else
        {
            //LastPlatfrom = Instantiate(PlatfromPre, LastPlatfrom.transform.position + new Vector3(0, 0, 1), Quaternion.identity);
            GameObject temp = PoolManager._Instance.GetObject("Platfrom");
            temp.transform.position = LastPlatfrom.transform.position + new Vector3(0, 0, 1);
            LastPlatfrom = temp;
        }
    }
    //碰撞退出检测
    private void OnCollisionExit(Collision collision)
    {
        PlatformSpwn();//再生成一个
        GameObject platform = collision.gameObject;
        platform.GetComponent<Rigidbody>().isKinematic = false;//将检测到的品台刚体中的isKinematic取消 就会下落
        InvokeDestroyPlatform(platform);//销毁
    }
    //开启协程
    void InvokeDestroyPlatform(GameObject go)
    {
        StartCoroutine(DestroyPlatform(go));
    }
    //等待一秒钟销毁物体
    IEnumerator DestroyPlatform(GameObject go)
    {
        yield return new WaitForSeconds(1f);//等待一分钟
        // Destroy(go);//销毁
        PoolManager._Instance.HideObject(go);
    }
}
